<template>
	<view class="content">
		<view class="top">
			<view class="one">
				<text class="title1">{{msgDict.photo}}</text>	
				<view class="head"  @click="changeImg">
				  <image class="headImg" :src="headImgPic"  mode="heightFix"></image>
				  <uni-icons type="arrowright" size="18" color="#ccc"></uni-icons>
				</view>
				
			</view>
			<view class="two" @click="btnNikeNameClick">
				<view class="labName">{{msgDict.nickname}}</view>
				<view class="labVule">
					{{gradeText}} <uni-icons type="arrowright" size="18" color="#ccc"></uni-icons>
				</view>
			</view>
			<view class="two" >
				<view class="labName">{{msgDict.mobile}}</view>
				<view class="labVule marginLeft">{{iphoneText}}</view>
			</view>
			<view class="two" @click="btnJianjieClick">
				<view class="labName">{{msgDict.jianjie}}</view>
				<view class="labVule">
					{{gradeText}} <uni-icons type="arrowright" size="18" color="#ccc"></uni-icons>
				</view>
			</view>
			<view class="two bottomZero">
				<view class="labName">{{msgDict.weixin}}</view>
				<view class="labVule">
					{{gradeText}} <uni-icons type="arrowright" size="18" color="#ccc"></uni-icons>
				</view>
			</view>
			<view class="line"></view>
			<view class="two" @click="btnGoAddrList">
				<view class="labName">{{msgDict.address}}</view>
				<view class="labVule">
				  <uni-icons type="arrowright" size="18" color="#ccc"></uni-icons>
				</view>
			</view>
			<view class="two" @click="btnChangeLanguageClick">
				<view class="labName" >{{msgDict.language}}</view>
				<view class="labVule">
					<uni-icons type="arrowright" size="18" color="#ccc"></uni-icons>
				</view>
			</view>
			<view class="two bottomZero">
				<view class="labName">{{msgDict.cache}}</view>
				<view class="labVule">
					{{gradeText}} <uni-icons type="arrowright" size="18" color="#ccc"></uni-icons>
				</view>
			</view>
			<view class="line"></view>
			<view class="two" @click="btnFeedBackClick">
				<view class="labName">{{msgDict.feedback}}</view>
				<view class="labVule">
				   <uni-icons type="arrowright" size="18" color="#ccc"></uni-icons>
				</view>
			</view>
			<view class="two" @click="btnPurchaseClick">
				<view class="labName">{{msgDict.purchase}}</view>
				<view class="labVule">
				  <uni-icons type="arrowright" size="18" color="#ccc"></uni-icons>
				</view>
			</view>
			<view class="two">
				<view class="labName">{{msgDict.score}}</view>
				<view class="labVule">
					<uni-icons type="arrowright" size="18" color="#ccc"></uni-icons>
				</view>
			</view>
			<view class="two" @click="btnVerSion">
				<view class="labName">{{msgDict.version}}</view>
				<view class="labVule">
				    V{{version}}<uni-icons type="arrowright" size="18" color="#ccc"></uni-icons>
				</view>
			</view>
			<view class="two" @click="btnGotoClick">
				<view class="labName">{{msgDict.us}}</view>
				<view class="labVule">
				  <uni-icons type="arrowright" size="18" color="#ccc"></uni-icons>
				</view>
			</view>
			<view class="line"></view>
		</view>
		<view class="btnGroup">
			<view class="exist showBtnBackColor" @click="btnLogOutClick">{{msgDict.logout}}</view>
		</view>
		
	
	</view>
</template>

<script>
	import {uesrEdit,uesrQueryById} from '../../api/index.js'
	export default {
		
		data() {
			return {
				userId:undefined,
				userInfo:{},
				version:'',
			    unionId:'',
				headImgPic:'../../static/universal_head_def.png',
				imageUploadUrl:'',
				token:'',
				nickName:'',
				iphoneText:'',
				gradeText:'',
				msgDict:{}
			
			}
		},
		onLoad() {
		 
	     this.msgDict = getApp().globalData.$t('setting');
		 this.userId = getApp().globalData.userId
		 this.getUesrQueryById();
		 this.imageUploadUrl = getApp().globalData.baseUrl +'/manage/sys/common/upload?isup=1'	
		 
		 
		 uni.getStorage({
		 	key:'userName',
			success: (resInfo) => {
				
				this.iphoneText = resInfo.data;
			}
		 })
		 
		 // #ifdef APP-PLUS
		   this.version = plus.runtime.version
		 // #endif
		 
		 

		},
		methods:{
			toChangeName() {
				// uni.navigateTo({
				// 	url:"/pages/mine/changeName?nickName=" + this.userInfo.nickName
				// })
			},
			async getMyCouponList(headerPic) {
				    try {
						let {result} = await uesrEdit({
							url:headerPic,
							id:this.userId,
						});
					     this.headImgPic = headerPic;
					
					} catch (err) {
						// console.log('zjk:', err);
					}
					
		
				
			},
			
			changeImg() {
				let that  =  this;
				uni.showActionSheet({
					itemList: ['拍照上传', '从相册中选择'],
					success: function(res) {
						if (res.tapIndex == 0) {
							//相册
							uni.chooseImage({
								count:1,
								sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
								sourceType: ['camera'], 
								success: function(res) {
									if (res.errMsg == 'chooseImage:ok') {
										console.log('tempFiles',res);
										if (res.tempFiles[0].size < 1024*1024) {
											that.asyncUpload(res.tempFilePaths[0]);
										} else{
											that.pressImages(res.tempFilePaths[0]);
										}
										
										
									}
								}
							})		
							
						} else if (res.tapIndex == 1) {
							
							uni.chooseImage({
								count: 1,
								sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
								sourceType: ['album'], //相册
								success: function(res) {
									if (res.errMsg == 'chooseImage:ok') {
										console.log('tempFiles',res);
										if (res.tempFiles[0].size < 1024*1024) {
											that.asyncUpload(res.tempFilePaths[0]);
										} else{
											that.pressImages(res.tempFilePaths[0]);
										}
									}
								}
							})		
						}
					},
					
					
					
					})
				
			},
			async getUesrQueryById() {
				    try {
					let {result} = await uesrQueryById({
			
						id:this.userId
					});
					
					if (result.url) {
					 this.headImgPic = result.url
					}else{
					 this.headImgPic = '../../static/universal_head_def.png'
					}
					
					} catch (err) {
						// console.log('zjk:', err);
					}
					
					
				
			},
			pressImages(oldUrl) {
			
				console.log('开始压缩');
				uni.compressImage({
					src: oldUrl,
					quality: 70, //压缩比例
					overwrite: true,
					success: res => {
						console.log('开始压缩结果',res);
						this.asyncUpload(res.tempFilePath);
					},
					error: res => {
						console.log('压缩',res);
						uni.showToast({
							icon:'none',
							title:'上传失败，请重新尝试'
						},1500)
					},
				
					
				});
			},
			asyncUpload(imgUrl) {
				console.log('开始上传',imgUrl,this.imageUploadUrl);
				uni.uploadFile({
					url:this.imageUploadUrl,
					filePath: imgUrl,
					// content-type:'multipart/form-data',
					name: 'file',
					success: (uploadFileRes) => {
						if (uploadFileRes.statusCode == 200) {
							
							let res = JSON.parse(uploadFileRes.data);
							console.log("上传多张：",uploadFileRes)
							/*注意 如果返回的结果字段不一样，请在这里做修改[res.code]*/
							if (res.code === 0 || res.code === '0') {
								//成功情况下
								console.log('成功1',res);
								
								this.getMyCouponList(res.message);
								
							} else {
								
								uni.showToast({
									icon:'none',
									title:'上传失败，请重新尝试'
								},1500)
							}
						
						} else {
							console.log('成功2',uploadFileRes);
						}
					},
					fail: (e) => {
						console.log('errorwe',e);
						uni.showToast({
							icon:'none',
							title:'上传失败，请重新尝试'
						},1500)
					}
				});
				
			},
			
			btnVerSion(){
				// #ifdef APP-PLUS
				if (plus.os.name == 'Android') {
					let appurl = 'market://details?id=io.dcloud.HelloH5';
					plus.runtime.openURL(appurl)
				} else{
					
					let appleId=12345678 //app的appleId
					plus.runtime.launchApplication({
						action: `itms-apps://itunes.apple.com/cn/app/id${appleId}?mt=8`
					}, function(e) {
						console.log('Open system default browser failed: ' + e.message);
					});
				}
				// #endif
			},
			btnNikeNameClick(){
				uni.navigateTo({
					url:'../changeNickname/changeNickname'
				})
			},
			btnJianjieClick(){
				uni.navigateTo({
					url:'../changeBriefInfo/changeBriefInfo'
				})
			},
			btnGoAddrList(){
				uni.navigateTo({
					url:'../addrList/addrList'
				})
			},
			btnGotoClick(){
				uni.navigateTo({
					url:'../aboutUs/aboutUs'
				})
			},
			btnPurchaseClick(){
			  //隐私协议
			  uni.navigateTo({
			  	url:'../policyInfo/policyInfo'
			  })	
			},
			btnFeedBackClick(){
				
				uni.navigateTo({
					url:'../feedback/feedback'
				})
			},
			btnChangeLanguageClick(){
				uni.navigateTo({
					url:'../changeLanguage/changeLanguage'
				})
			},
			btnLogOutClick(){
				
				setTimeout(()=>{			   
					  uni.switchTab({
						 url:'/pages/index/index',
						 
					  }) 
									   
				},400)
				uni.showToast({
					icon:'success',
					title:'退出成功'
				})
				this.userName=uni.getStorageSync('userName');
				
				// #ifdef APP-PLUS
				
				this.unbind(this.userName)
				
				// #endif
				
				getApp().globalData.stuId='';
				getApp().globalData.AppLogin=false;
				getApp().globalData.userId='';
				getApp().globalData.appSchoolId='';
				uni.removeStorageSync('appSchoolId')
				uni.removeStorageSync('userId')
				uni.removeStorageSync('stuId')
				uni.removeStorageSync('needAddChild')
				uni.removeStorageSync('userName')
				// uni.removeStorageSync('openId')
				// this.logOut({unionId:this.unionId,token:this.token})
				
			},
			unbind(alias) {
				var isAndorid, PushManager, context, Instance, GeTuiSdk;
				 
				        if (plus.os.name == 'Android') {
				            isAndorid = true;
				        } else {
				            isAndorid = false;
				        }
				 
				        if (isAndorid) {
				            PushManager = plus.android.importClass("com.igexin.sdk.PushManager");
				            context = plus.android.runtimeMainActivity().getContext();
				            Instance = PushManager.getInstance();
				        } else {
				            GeTuiSdk = plus.ios.importClass("GeTuiSdk");
				        }
				if (isAndorid) {
					Instance.unBindAlias(context, alias, true);
				} else {
					GeTuiSdk.unbindAliasandSequenceNumandIsSelf(alias, alias, true);
				}
			},
		
			async logOut(dict){
			 
					let {data} = await logOut(dict);
				  
				   setTimeout(()=>{
					   
					  uni.switchTab({
					     url:'/pages/index/index',
					     
					  }) 
					   
				   },400)
				   uni.showToast({
				   	icon:'success',
					title:'退出成功'
				   })
                   getApp().globalData.user={};
				   getApp().globalData.AppLogin=false;
				   getApp().globalData.userId='';
				   getApp().globalData.token='';
				   uni.removeStorageSync('token')
				   uni.removeStorageSync('userId')
				   uni.removeStorageSync('needAddChild')
				   uni.removeStorageSync('userName')
				   uni.removeStorageSync('stuId')
				   uni.removeStorageSync('appSchoolId')
				   
				   
			
			}
			
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #F2F6FC;
	}
	.content {
		background-color: #F6F6F6;
		display: flex;
		flex-direction: column;

		justify-content: center;
		.top {
			background-color: #FFFFFF;
			padding-left: 20upx;
			padding-right: 20upx;
			padding-bottom: 180upx;
			padding-bottom: calc(180upx + constant(safe-area-inset-bottom));  
			padding-bottom: calc(180upx + env(safe-area-inset-bottom));

			.one {
				background-color: #FFFFFF;
				height: 110upx;
				border-bottom: 1upx solid #f2f2f2;
				display: flex;
				flex-direction: row;
				justify-content: space-between;
				align-items: center;

				.title1 {

					color: #333333;
					font-size: 32upx;
				}
                .head {
                	display: flex;
                	align-items: center;
                	
                }
				.headImg {
					height: 90rpx;
					width: 90rpx;
					border-radius: 50%;
					// background-color: #007AFF;
				}
				
			}

			.two {
				background-color: #FFFFFF;
				height: 110upx;
				border-bottom: thin solid #f2f2f2;
				display: flex;
				flex-direction: row;
				justify-content: space-between;
				align-items: center;
				font-size: 32rpx;
				color: #333;
			}
			.bottomZero{
				border-bottom: 0rpx solid #f2f2f2;
			}
			.marginLeft{
				margin-right: 15rpx;
			}
			.line{
				height: 20rpx;
				background-color: #F2F6FC;
				margin: 0 -20rpx 0;
			}
		}
       .btnGroup{
		   background-color: #F2f2f2;
		   // height: 100rpx;
		   position: fixed;
		   bottom: 0;
		   right: 0;
		   width: 750rpx;
		   padding-top:40rpx;
		   padding-bottom: 20upx;
		   padding-bottom: calc(20rpx + constant(safe-area-inset-bottom));  
		   padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
		.exist {
			margin: 20upx auto;
			width: 515upx;
			height: 80upx;
			// background: #DD524D;
			border-radius: 40upx;

			color: #FFFFFF;
			font-size: 32upx;
			text-align: center;
			line-height: 80rpx;
		}
		
		}
	}
	
</style>
